import { Controller } from 'egg';
import { EditNicknameIn, LoginIn, RegisterIn } from '../dto/in/memberIn';
import { LoginOut } from '../dto/out/memberOut';
import ApiDecorator from '../src/decorator/apiDecorator';
import ResultUtil from '../src/util/resultUtil';

@ApiDecorator.Controller("/member", "用户模块")
export default class MemberController extends Controller {

    @ApiDecorator.POST("/register", "用户注册", { in: RegisterIn, auth: false })
    public async register(params: RegisterIn) {
        await this.service.member.register(params);
        this.ctx.body = ResultUtil.successMsg("注册成功");
    }

    @ApiDecorator.POST("/login", "用户登录", { in: LoginIn, out: LoginOut, auth: false })
    public async login(params: LoginIn) {
        this.ctx.body = ResultUtil.success(await this.service.member.login(params));
    }

    /**获取用户信息 */
    @ApiDecorator.GET("/info", "用户信息", {})
    public async getMemberInfo() {
        this.ctx.body = ResultUtil.success(await this.service.member.memberInfoById());
    }

    @ApiDecorator.PUT("/nickname", "修改昵称", { in: EditNicknameIn })
    public async editNickname(params: EditNicknameIn) {
        await this.service.member.editNickname(params.nickname);
        this.ctx.body = ResultUtil.successMsg();
    }
}
